#include <QtGui>

#include "glwidget.h"
#include "mainwindow.h"

 CMainWindow::CMainWindow(string cellfile)
 {
     QMessageBox msgBox;

     glWidget = new GLWidget;
     string redcellfile, greencellfile, bluecellfile;
     redcellfile = cellfile;

     //
     QString temp;
     greencellfile = QString::fromStdString(cellfile).replace("Red", "Green").toStdString();
     bluecellfile = QString::fromStdString(cellfile).replace("Red", "Blue").toStdString();

     //greencellfile = cellfile.replace("red", "green");
     //bluecellfile = cellfile.replace("red", "blue");
     //

     msgBox.setText(QString::fromStdString(cellfile+" "+greencellfile+" "+bluecellfile));
     //msgBox.setInformativeText("Do you want to save your changes?");
     msgBox.setStandardButtons(QMessageBox::Ok);
     msgBox.setDefaultButton(QMessageBox::Ok);
     int ret = msgBox.exec();

     glWidget->cell.load(redcellfile, greencellfile, bluecellfile);
     glWidget->cell.genMask();
     glWidget->cell.getDelta();


     LMain = new QLabel ("Cell information:");
     L1 = new QLabel ("X centroid:");
     L2 = new QLabel ("Y centroid:");
     L3 = new QLabel ("Mean radius: ");
     L4 = new QLabel ("Sphericity: ");
     L5 = new QLabel ("Eccentricity: ");
     L6 = new QLabel ("Inertia shape: ");
     L7 = new QLabel ("Compactness: ");

     QVBoxLayout *xcent = new QVBoxLayout;
     QVBoxLayout *ycent = new QVBoxLayout;
     QHBoxLayout *centrs = new QHBoxLayout;

     ed1 = new QTextEdit ();
     ed2 = new QTextEdit ();
     ed3 = new QTextEdit ();
     ed4 = new QTextEdit ();
     ed5 = new QTextEdit ();
     ed6 = new QTextEdit ();
     ed7 = new QTextEdit ();
     ed1->setReadOnly(true);
     ed2->setReadOnly(true);
     ed3->setReadOnly(true);
     ed4->setReadOnly(true);
     ed5->setReadOnly(true);
     ed6->setReadOnly(true);
     ed7->setReadOnly(true);
     ed1->setFixedHeight(23);
     ed2->setFixedHeight(23);
     ed3->setFixedHeight(23);
     ed4->setFixedHeight(23);
     ed5->setFixedHeight(23);
     ed6->setFixedHeight(23);
     ed7->setFixedHeight(23);
     QString xc, yc, mr, sph, ec, ish, co;

     xc.setNum(glWidget->cell.x_centr(), 'f', 6);
     yc.setNum(glWidget->cell.y_centr(), 'f', 6);
     mr.setNum(glWidget->cell.mean_radius(), 'f', 6);
     sph.setNum(glWidget->cell.sphericity()*100, 'f', 1);
     ec.setNum(glWidget->cell.eccentricity(), 'f', 6);
     ish.setNum(glWidget->cell.inertia_shape(), 'f', 6);
     co.setNum(glWidget->cell.compactness(), 'f', 6);
     sph= sph + " %";
     ed1->setText(xc);
     ed2->setText(yc);
     ed3->setText(mr);
     ed4->setText(sph);
     ed5->setText(ec);
     ed6->setText(ish);
     ed7->setText(co);


     QString ann;
     switch (this->getAnnResult()) {
     case 0:
         ann = "Most probably its BC";
         break;
     case 1:
         ann = "Most probably its FAN";
         break;
     case 2:
         ann = "Most probably its Control";
         break;
     case -1:
         ann = "Something go wrong! :(";
         break;
     }

     L8 = new QLabel (ann);

     xcent->addWidget(L1);
     xcent->addWidget(ed1);
     ycent->addWidget(L2);
     ycent->addWidget(ed2);

     centrs->addLayout(xcent);
     centrs->addLayout(ycent);

     BClose = new QPushButton("Close");

     connect(BClose, SIGNAL(clicked()), this, SLOT(close()));

     BCount = new QPushButton("Count");
     BCount->setVisible(false);

     QHBoxLayout *buttons = new QHBoxLayout;
     buttons->addWidget(BCount);
     buttons->addWidget(BClose);

     QVBoxLayout *mainlayout = new QVBoxLayout;

     QHBoxLayout *toplayout = new QHBoxLayout;
        toplayout->addWidget(LMain);

     QHBoxLayout *bottomlayout = new QHBoxLayout;
     QVBoxLayout *bottomrlayout = new QVBoxLayout;

     bottomrlayout->addLayout(centrs);
     bottomrlayout->addWidget(L3);
     bottomrlayout->addWidget(ed3);
     bottomrlayout->addWidget(L4);
     bottomrlayout->addWidget(ed4);
//   bottomrlayout->addWidget(L5);
//   bottomrlayout->addWidget(ed5);
     bottomrlayout->addWidget(L6);
     bottomrlayout->addWidget(ed6);
     bottomrlayout->addWidget(L7);
     bottomrlayout->addWidget(ed7);
     bottomrlayout->addWidget(L8);
     bottomrlayout->addStretch();
     bottomrlayout->addLayout(buttons);

     QVBoxLayout *bottomllayout = new QVBoxLayout;
     bottomllayout->addWidget(glWidget);

     bottomlayout->addLayout(bottomllayout);
     bottomlayout->addLayout(bottomrlayout);

     mainlayout->addLayout(toplayout);
     mainlayout->addLayout(bottomlayout);
     setLayout(mainlayout);

     //glWidget->paintGL();
     setWindowTitle(tr("Cell information"));
     //glWidget->cell.OD_var();
 }

int CMainWindow::getAnnResult(){
    qDebug() << "Satrted ann recog";
    int annResult = -1;
    vector<double> signs;
    signs = glWidget->cell.getSignals();
    float *frequencies = new float[signs.size()];
    float *output;

    FILE *scaleValues = fopen ("..\\Patients\\scaleValues.dat", "r");
    vector<double> max, min;
    max.resize(signs.size());
    min.resize(signs.size());
    for (int i=0; i<signs.size(); i++)
    {
        double temp;
        fscanf (scaleValues, "%f", &temp);
        max.push_back(temp);
    }
    for (int i=0; i<signs.size(); i++)
    {
        double temp;
        fscanf (scaleValues, "%f", &temp);
        min.push_back(temp);
    }
    fclose(scaleValues);


    for (int i=0; i< signs.size(); i++)
        if (max[i]-min[i]){
            frequencies[i] = 0;
        } else {
            frequencies[i] = (float)signs[i]-min[i]/(max[i]-min[i]);
        }

    try {
        struct fann *ann = fann_create_from_file("..\\net\\cell_classify.net");
        output = fann_run(ann, frequencies);

        if (output[0]>output[1]) {
            if (output[0]>output[2])
                return 0;
        } else {
            if (output[1]>output[2])
                return 1;
            else return 2;
        }
    } catch(...) {
        qDebug() << "Problems with ann";
    }

    return annResult;
}

 void CMainWindow::keyPressEvent(QKeyEvent *e)
 {
     if (e->key() == Qt::Key_Escape)
         close();
     else
         QWidget::keyPressEvent(e);
 }
